# PSA_ReadVoltages.py
"""
  Reads voltages current and displays them.
  Example: 
      python ReadVoltages 5 6
  This will read all the voltage values in COM5 and COM6.
  Can provide as many com ports as wanted but needs at least one.
9/4/2013, John Kwong
"""
import numpy as np
import serial
import time
import sys
import PSA_HighVoltage

def GetHVMeasurements(*arg):
    # Get the port numbers
    portNumbers = []
    if len(arg) < 1:
        print('No COM port given. Quitting.')
        return
    else:
        # get the port numbers
        argList = arg[0]
        for (index, a) in enumerate(argList):
            portNumbers.append(int(a))  # port numbers start at 0 in pytyon
    
    # cycle through the port numbers
    for (portIndex, portNum) in enumerate(portNumbers):
        print('Reading hv values from COM%d' %(portNum))
        hv = PSA_HighVoltage.HighVoltageDevice('PSA1', portNum)
        hv.Connect()
        (vm, vs, vp, c) = hv.ReadVoltages()
        hv.Disconnect()
        print('%d\t%s\t%s\t%s\t%s' %('Det#', 'V set', 'V', 'V preset0', 'I'))
        for i in xrange(len(vm)):
            print('%d\t%3.2f\t%3.2f\t%3.2f\t%3.2f' %(i+1, vs[i], vm[i], vp[i], c[i]))
        
if __name__ == "__main__":
    GetHVMeasurements(sys.argv[1:])